﻿using System;
using System.Threading.Tasks;
using System.Windows;
using TaskClient5.SystemService;
using TaskClient5.UserService;
using System.Threading;

namespace TaskClient5
{
    public partial class MainPage
    {
        private readonly UserServiceClient _userServiceClient = new UserServiceClient();
        private readonly SystemServiceClient _systemServiceClient = new SystemServiceClient();

        public MainPage()
        {
            InitializeComponent();
            Loaded += OnMainPageLoaded;
        }

        //async private void OnMainPageLoaded(object sender, RoutedEventArgs e)
        //{
        //    var users = await userServiceClient.GetAllTaskAsync();
        //    dataGrid1.ItemsSource = users;
        //}

        //async private void OnGetTimeClick(object sender, RoutedEventArgs e)
        //{
        //    var time = await systemServiceClient.GetSerivceTimeTaskAsync();
        //    MessageBox.Show(time.ToString());
        //}

        //async private void OnHiClick(object sender, RoutedEventArgs e)
        //{
        //    var result = await systemServiceClient.SayHiToTaskAsync("Tom");
        //    MessageBox.Show(result);
        //}

        //async private void OnException1Click(object sender, RoutedEventArgs e)
        //{
        //    var result = await systemServiceClient.SayHiToTaskAsync(null);
        //    MessageBox.Show(result);
        //}

        //async private void OnException2Click(object sender, RoutedEventArgs e)
        //{
        //    var num = await systemServiceClient.GetNumTaskAsync(0);
        //    MessageBox.Show(num.ToString());
        //}

        //async private void OnMultiTaskClick(object sender, RoutedEventArgs e)
        //{
        //    var num = await systemServiceClient.GetNumTaskAsync(3);
        //    var result = await systemServiceClient.SayHiToTaskAsync(num.ToString("n2"));
        //    var time = await systemServiceClient.GetSerivceTimeTaskAsync();
        //    var users = await userServiceClient.GetAllTaskAsync();
        //    MessageBox.Show(string.Format("Num:{1},{0}{2},{0}Time:{3},{0}UserCount:{4}", Environment.NewLine,
        //                       num.ToString("n2"),
        //                       result,
        //                       time.ToString(),
        //                       users.Count.ToString()));
        //}

        //没有安装atp的使用这些方法

        private void OnMainPageLoaded(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(s =>
            {
                var task = _userServiceClient.GetAllTaskAsync();
                task.Wait();
                Dispatcher.BeginInvoke(() =>
                {
                    dataGrid1.ItemsSource = task.Result;
                });
            });
        }

        private void OnGetTimeClick(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(s =>
            {
                var task = _systemServiceClient.GetSerivceTimeTaskAsync();
                task.Wait();
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(task.Result.ToString());
                });
            });
        }

        private void OnHiClick(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(s =>
            {
                var task = _systemServiceClient.SayHiToTaskAsync("Tom");
                task.Wait();
                Dispatcher.BeginInvoke(() => MessageBox.Show(task.Result));
            });
        }

        private void OnException1Click(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(s =>
            {
                var task = _systemServiceClient.SayHiToTaskAsync(null);
                task.Wait();
                Dispatcher.BeginInvoke(() => MessageBox.Show(task.Result));
            });
        }

        private void OnException2Click(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(s =>
            {
                var task = _systemServiceClient.GetNumTaskAsync(0);
                task.Wait();
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(task.Result.ToString());
                });
            });
        }

        private void OnMultiTaskClick(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(s =>
            {
                var task1 = _systemServiceClient.GetNumTaskAsync(5);
                task1.Wait();
                var task2 = _systemServiceClient.SayHiToTaskAsync(task1.Result.ToString());
                var task3 = _systemServiceClient.GetSerivceTimeTaskAsync();
                var task4 = _userServiceClient.GetAllTaskAsync();
                Task.WaitAll(task2, task3, task4);
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(string.Format("Num:{1},{0}{2},{0}Time:{3},{0}UserCount:{4}", Environment.NewLine,
                        task1.Result.ToString(),
                        task2.Result,
                        task3.Result.ToString(),
                        task4.Result.Count.ToString()));
                });
            });
        }
    }
}